package com.lge.clock.worldclock;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.lge.clock.worldclock.util.DatabaseHelper;

/**
 * @author lge.com
 */
public class WorldClockProvider extends ContentProvider {
	private static final String WORLDCLOCKURI = "content://com.lge.clock.worldclock.WorldClockProvider/WorldClockList";
	
	private static final int WC = 1;
	private static final int WC_ID = 2;
	private static final UriMatcher URIMATCHER;
	
	/**
	 * Field WORLDCLOCK_CONTENT_URI.
	 */
	public static final Uri WORLDCLOCK_CONTENT_URI = Uri.parse(WORLDCLOCKURI);
	private static HashMap<String, String>  worldClock_list_projection;
	
	private SQLiteDatabase worldClockDB;
	
	private SQLiteOpenHelper wdb;
	
	static {
		URIMATCHER = new UriMatcher(UriMatcher.NO_MATCH);
		URIMATCHER.addURI("com.lge.clock.worldclock.WorldClockProvider", "WorldClockList", WC);
		URIMATCHER.addURI("com.lge.clock.worldclock.WorldClockProvider", "WorldClockList/#", WC_ID);
		
		worldClock_list_projection=new HashMap<String, String>();
		worldClock_list_projection.put("_id", "_id");
		worldClock_list_projection.put("country_en", "country_en");
		worldClock_list_projection.put("country_ko", "country_ko");
		worldClock_list_projection.put("city_en", "city_en");
		worldClock_list_projection.put("city_ko", "city_ko");
		worldClock_list_projection.put("gmt_value", "gmt_value");
		worldClock_list_projection.put("latitude", "latitude");
		worldClock_list_projection.put("longitude", "longitude");
		worldClock_list_projection.put("diplay_level", "diplay_level");
		worldClock_list_projection.put("label_position", "label_position");
		worldClock_list_projection.put("daylight", "daylight");
		worldClock_list_projection.put("request", "request");
		worldClock_list_projection.put("setinfo", "setinfo");
		worldClock_list_projection.put("country_bg", "country_bg");
		worldClock_list_projection.put("city_bg", "city_bg");
		worldClock_list_projection.put("country_hr", "country_hr");
		worldClock_list_projection.put("city_hr", "city_hr");
		worldClock_list_projection.put("country_cs", "country_cs");
		worldClock_list_projection.put("city_cs", "city_cs");
		worldClock_list_projection.put("country_da", "country_da");
		worldClock_list_projection.put("city_da", "city_da");
		worldClock_list_projection.put("country_nl", "country_nl");
		worldClock_list_projection.put("city_nl", "city_nl");
		worldClock_list_projection.put("country_nl_BE", "country_nl_BE");
		worldClock_list_projection.put("city_nl_BE", "city_nl_BE");
		worldClock_list_projection.put("country_fi", "country_fi");
		worldClock_list_projection.put("city_fi", "city_fi");
		worldClock_list_projection.put("country_fr", "country_fr");
		worldClock_list_projection.put("city_fr", "city_fr");
		worldClock_list_projection.put("country_fr_BE", "country_fr_BE");
		worldClock_list_projection.put("city_fr_BE", "city_fr_BE");
		worldClock_list_projection.put("country_fr_CH", "country_fr_CH");
		worldClock_list_projection.put("city_fr_CH", "city_fr_CH");
		worldClock_list_projection.put("country_de", "country_de");
		worldClock_list_projection.put("city_de", "city_de");
		worldClock_list_projection.put("country_de_CH", "country_de_CH");
		worldClock_list_projection.put("city_de_CH", "city_de_CH");
		worldClock_list_projection.put("country_el", "country_el");
		worldClock_list_projection.put("city_el", "city_el");
		worldClock_list_projection.put("country_hu", "country_hu");
		worldClock_list_projection.put("city_hu", "city_hu");
		worldClock_list_projection.put("country_is", "country_is");
		worldClock_list_projection.put("city_is", "city_is");
		worldClock_list_projection.put("country_it", "country_it");
		worldClock_list_projection.put("city_it", "city_it");
		worldClock_list_projection.put("country_it_CH", "country_it_CH");
		worldClock_list_projection.put("city_it_CH", "city_it_CH");
		worldClock_list_projection.put("country_mk", "country_mk");
		worldClock_list_projection.put("city_mk", "city_mk");
		worldClock_list_projection.put("country_no", "country_no");
		worldClock_list_projection.put("city_no", "city_no");
		worldClock_list_projection.put("country_pl", "country_pl");
		worldClock_list_projection.put("city_pl", "city_pl");
		worldClock_list_projection.put("country_pt", "country_pt");
		worldClock_list_projection.put("city_pt", "city_pt");
		worldClock_list_projection.put("country_ro", "country_ro");
		worldClock_list_projection.put("city_ro", "city_ro");
		worldClock_list_projection.put("country_sr", "country_sr");
		worldClock_list_projection.put("city_sr", "city_sr");
		worldClock_list_projection.put("country_sk", "country_sk");
		worldClock_list_projection.put("city_sk", "city_sk");
		worldClock_list_projection.put("country_sl", "country_sl");
		worldClock_list_projection.put("city_sl", "city_sl");
		worldClock_list_projection.put("country_es", "country_es");
		worldClock_list_projection.put("city_es", "city_es");
		worldClock_list_projection.put("country_sv", "country_sv");
		worldClock_list_projection.put("city_sv", "city_sv");
		worldClock_list_projection.put("country_tr", "country_tr");
		worldClock_list_projection.put("city_tr", "city_tr");
		worldClock_list_projection.put("country_ru", "country_ru");
		worldClock_list_projection.put("city_ru", "city_ru");
		worldClock_list_projection.put("country_ru_rIL", "country_ru_rIL");
		worldClock_list_projection.put("city_ru_rIL", "city_ru_rIL");
		worldClock_list_projection.put("country_ar", "country_ar");
		worldClock_list_projection.put("city_ar", "city_ar");
		worldClock_list_projection.put("country_iw", "country_iw");
		worldClock_list_projection.put("city_iw", "city_iw");
		worldClock_list_projection.put("country_zh_CN", "country_zh_CN");
		worldClock_list_projection.put("city_zh_CN", "city_zh_CN");
		worldClock_list_projection.put("country_zh_TW", "country_zh_TW");
		worldClock_list_projection.put("city_zh_TW", "city_zh_TW");
		worldClock_list_projection.put("country_pt_BR", "country_pt_BR");
		worldClock_list_projection.put("city_pt_BR", "city_pt_BR");
		worldClock_list_projection.put("country_es_rMX", "country_es_rMX");
		worldClock_list_projection.put("city_es_rMX", "city_es_rMX");
		worldClock_list_projection.put("country_fr_CA", "country_fr_CA");
		worldClock_list_projection.put("city_fr_CA", "city_fr_CA");
		worldClock_list_projection.put("country_en_us", "country_en_us");
		worldClock_list_projection.put("city_en_us", "city_en_us");
		worldClock_list_projection.put("country_en_au", "country_en_au");
		worldClock_list_projection.put("city_en_au", "city_en_au");
		worldClock_list_projection.put("country_en_ca", "country_en_ca");
		worldClock_list_projection.put("city_en_ca", "city_en_ca");
		worldClock_list_projection.put("country_en_ie", "country_en_ie");
		worldClock_list_projection.put("city_en_ie", "city_en_ie");
		worldClock_list_projection.put("country_zh_hk", "country_zh_hk");
		worldClock_list_projection.put("city_zh_hk", "city_zh_hk");
		worldClock_list_projection.put("country_ms", "country_ms");
		worldClock_list_projection.put("city_ms", "city_ms");
		worldClock_list_projection.put("country_id", "country_id");
		worldClock_list_projection.put("city_id", "city_id");
		worldClock_list_projection.put("country_th", "country_th");
		worldClock_list_projection.put("city_th", "city_th");
		worldClock_list_projection.put("country_vi", "country_vi");
		worldClock_list_projection.put("city_vi", "city_vi");
		worldClock_list_projection.put("country_fa", "country_fa");
		worldClock_list_projection.put("city_fa", "city_fa");
		worldClock_list_projection.put("country_ja", "country_ja");
		worldClock_list_projection.put("city_ja", "city_ja");
		worldClock_list_projection.put("country_es_AR", "country_es_AR");  //[suhaeng.lee] add language es_AR for VIVO
		worldClock_list_projection.put("city_es_AR", "city_es_AR");
		worldClock_list_projection.put("country_kk", "country_kk");
		worldClock_list_projection.put("city_kk", "city_kk");
		worldClock_list_projection.put("country_uk", "country_uk");
		worldClock_list_projection.put("city_uk", "city_uk");
	}
	
	/**
	 * Method delete.
	 * @return int
	 */
	@Override
	public int delete(Uri uri, String where, String[] whereArgs) {
		worldClockDB = wdb.getWritableDatabase();
		int count;
	    
	    switch (URIMATCHER.match(uri)) {
	      case WC:
	        count = worldClockDB.delete(DatabaseHelper.PROVIDER_DATABASE_TABLE_NAME, where, whereArgs);
	        break;

	      case WC_ID:
	        String segment = uri.getPathSegments().get(1);
	        count = worldClockDB.delete(DatabaseHelper.PROVIDER_DATABASE_TABLE_NAME, "_id" + "="
	                                    + segment
	                                    + (!TextUtils.isEmpty(where) ? " AND (" 
	                                    + where + ')' : ""), whereArgs);
	        break;

	      default:
	    	  IllegalArgumentException iaExep = new IllegalArgumentException("Unsupported URI: " + uri);
	    	  throw iaExep;
	    }

	    getContext().getContentResolver().notifyChange(uri, null);
	    return count;
	}

	/**
	 * Method getType.
	 */
	@Override
	public String getType(Uri uri) {
		return null;
	}

	/**
	 * Method insert.
	 */
	@Override
	public Uri insert(Uri u, ContentValues values) {
		Log.i("WorldClockProvider", "insert");
		worldClockDB = wdb.getWritableDatabase();
		long rowID = worldClockDB.insert(DatabaseHelper.PROVIDER_DATABASE_TABLE_NAME, null, values);
        
	    // Return a URI to the newly inserted row on success.
		if (rowID > 0) {
			Uri uri = ContentUris.withAppendedId(WORLDCLOCK_CONTENT_URI, rowID);
			getContext().getContentResolver().notifyChange(uri, null);
			return uri;
		}
		getContext().getContentResolver().notifyChange(u, null);
		SQLException sqlEx = new SQLException("Failed to insert row into " + u + ", " + values);
		throw sqlEx;
	}

	/**
	 * Method onCreate.
	 * @return boolean
	 */
	@Override
	public boolean onCreate() {
		Context context = getContext();
		
		wdb = new DatabaseHelper(context, DatabaseHelper.PROVIDER_DATABASE_NAME);
		
		return true;
	}

	/**
	 * Method query.
	 */
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		worldClockDB = wdb.getWritableDatabase();
		switch(URIMATCHER.match(uri)) {
		
		case WC : 
			qb.setTables(DatabaseHelper.PROVIDER_DATABASE_TABLE_NAME);
			qb.setProjectionMap(worldClock_list_projection);
			break;
		case WC_ID:
			qb.setTables(DatabaseHelper.PROVIDER_DATABASE_TABLE_NAME);
			qb.appendWhere(  "_id="+uri.getPathSegments().get(1)  );
		default :
		}
	    // Apply the query to the underlying database.
	    Cursor c = qb.query(worldClockDB, 
	                        projection, 
	                        selection, selectionArgs, 
	                        null, null, sortOrder);
	    
	    if(c != null) {
	    	c.setNotificationUri(getContext().getContentResolver(), uri);
	    	
	    }
	    
	    return c;
	}
	
	/**
	 * Method update.
	 * @return int
	 */
	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		worldClockDB = wdb.getWritableDatabase();
		int count = 0;
		long rowId = 0;
		
		switch (URIMATCHER.match(uri)) {
	      case WC:
	    	  count = worldClockDB.update(DatabaseHelper.PROVIDER_DATABASE_TABLE_NAME, values, selection,  selectionArgs);
	    	  break;
	         
	      case WC_ID:
	    	  String segment = uri.getPathSegments().get(1);
              rowId = Long.parseLong(segment);
              count = worldClockDB.update(DatabaseHelper.PROVIDER_DATABASE_TABLE_NAME, values, "_id=" + rowId, null);
              break;
	      default :
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

}
